package com.efast.dmom.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsStorage;
import com.efast.dmom.mes.bean.WmsStorageLog;
import com.efast.dmom.mes.dao.entity.WmsInWarehouseOrderDao;
import com.efast.dmom.mes.dao.entity.WmsStorageDao;
import com.efast.dmom.mes.dao.entity.WmsStorageLogDao;
import com.efast.dmom.mes.entity.PartEntity;
import com.efast.dmom.mes.entity.WmsWarehouseBasEntity;
import com.efast.dmom.mes.service.WmsStorageService;
import com.efast.dmom.mes.util.ServiceBaseEntityUtil;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;
@Transactional
@Service
public class WmsStorageServiceImpl implements WmsStorageService {

	@Autowired
	private WmsStorageDao wmsStorageDao;

	@Autowired
	private WmsStorageLogDao wmsStorageLogDao;

	@Autowired
	private WmsInWarehouseOrderDao wmsInWarehouseOrderDao;

	@Override
	public ResultBean adjustStorageQuality(List<WmsStorage> list) {
		ResultBean resultBean = new ResultBean(true);
		// TODO Auto-generated method stub
		// 更新库存信息和库存总金额
		for (WmsStorage wmsStorage : list) {
			double sum = wmsStorage.getUnitPrice() * wmsStorage.getStorageQuality();
			wmsStorage.setTotalAmount(sum);
			ServiceBaseEntityUtil.setUpdateBaseEntityData(wmsStorage);
		}
		int batchUpdateQuality = wmsStorageDao.batchUpdateQuality(list);
		List<WmsStorageLog> logList = new ArrayList<WmsStorageLog>();
		if (batchUpdateQuality > 0) {
			for (WmsStorage wmsStorage : list) {
				// 更新库存调整历史表
				WmsStorage selectById = wmsStorageDao.selectById(wmsStorage);
				WmsStorageLog wmsStorageLog = new WmsStorageLog();
				ServiceBaseEntityUtil.setUpdateLogEntityData(wmsStorageLog);
				ServiceBaseEntityUtil.setUpdateBaseEntityData(wmsStorageLog);
				wmsStorageLog.setPartId(selectById.getPartId());
				wmsStorageLog.setWarehouseId(selectById.getWarehouseBasId());
				wmsStorageLog.setOriginalQuality(wmsStorage.getOriginalQuality());
				wmsStorageLog.setCurrentQuality(wmsStorage.getStorageQuality());
				wmsStorageLog.setUnit(wmsStorage.getUnit());
				wmsStorageLog.setUnitPrice(wmsStorage.getUnitPrice());
				wmsStorageLog.setRemark(wmsStorage.getRemark());
				wmsStorageLog.setUnitPrice(wmsStorage.getUnitPrice());
				logList.add(wmsStorageLog);
			}
			// 更新历史表数据
			wmsStorageLogDao.batchSave(logList);
		}
		return resultBean;
	}

	@Override
	public ResultBean validateDeleteInfo(WmsStorage wmsStorage) {
		ResultBean resultBean = new ResultBean(true);
		// 校验库存中是否存在当前物料
		int byWarehouseId = wmsStorageDao.getByWarehouseId(wmsStorage.getWarehouseBasId());
		if (byWarehouseId > 0) {
			resultBean.setErrorMessage("库存中存在当前仓库，无法删除！");
		}
		int byWarehouseId2 = wmsInWarehouseOrderDao.getByWarehouseId(wmsStorage.getWarehouseBasId());
		if (byWarehouseId2 > 0) {
			resultBean.setErrorMessage("入库单中存在当前仓库，无法删除！");
		}
		return resultBean;
	}

	@Override
	public ResultBean deleteWarehouseById(WmsStorage wmsStorage) {
		ResultBean resultBean = new ResultBean(true);
		ResultBean validateDeleteInfo = validateDeleteInfo(wmsStorage);
		if (!validateDeleteInfo.success) {
			return validateDeleteInfo;
		}
		wmsStorage.setLastUpdateTime(TimeUtil.getLocalDateTime());
		wmsStorage.setLastUpdateUser(SpringWebUtils.getRequestUser());
		wmsStorageDao.deleteWarehouseById(wmsStorage);
		return resultBean;
	}

	// 当为首工序时，扣减毛坯件数量
	@Override
	public ResultBean deductionPiecePartQty(long blankPiecePartId, int blankPiecePartQty, long warehouseId,double blankPiecePartUnitPrice) {
		ResultBean resultBean = new ResultBean(true, " ");
		WmsStorage wmsStorage = wmsStorageDao.getWmsStorageByPartId(blankPiecePartId, warehouseId);
		int qty = (int) wmsStorage.getStorageQuality();
		double totalAmount = wmsStorage.getTotalAmount();
		int deductionsQty = qty - blankPiecePartQty;
		if (deductionsQty < 0) {
			throw new RuntimeException("毛坯件库存不足，无法扣减！");
//			resultBean.setSuccess(false);
//			resultBean.setMessage("毛坯件库存不足，无法扣减！");
//			return resultBean;
		}
		double amount = totalAmount - blankPiecePartUnitPrice * blankPiecePartQty;
		if (amount < 0) {
			throw new RuntimeException("毛坯件总金额为"+totalAmount+"实际扣减金额"+blankPiecePartUnitPrice * blankPiecePartQty+"，无法扣减！");
		}
		wmsStorage.setStorageQuality(deductionsQty);
		wmsStorage.setTotalAmount(amount);
		wmsStorage.setLastUpdateTime(TimeUtil.getLocalDateTime());
		wmsStorage.setLastUpdateUser(SpringWebUtils.getRequestUser());
		wmsStorageDao.updateStorage(wmsStorage);
		resultBean.setSuccess(true);
		return resultBean;
	}

	@Override
	public List<PartEntity> getPartEntityList(long warehouseBasId) {
		List<PartEntity> partEntityList = wmsStorageDao.getPartEntityList(warehouseBasId);
		return partEntityList;
	}

	@Override
	public ResultBean save(WmsWarehouseBasEntity wmsWarehouseBasEntity) {
		ResultBean resultBean = new ResultBean(true, " ");
		WmsWarehouseBasEntity entityByCode = wmsStorageDao.getByCode(wmsWarehouseBasEntity.getWarehouseNo());
		if (entityByCode != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("仓库编号不唯一！");
			return resultBean;
		}
		WmsWarehouseBasEntity entityByName = wmsStorageDao.getByName(wmsWarehouseBasEntity.getWarehouseName());
		if (entityByName != null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("仓库名称不唯一！");
			return resultBean;
		}
		wmsWarehouseBasEntity.setCompanyCode(SpringWebUtils.getRequestCompany());
		wmsWarehouseBasEntity.setCreateTime(TimeUtil.getLocalDateTime());
		wmsWarehouseBasEntity.setCreateUser(SpringWebUtils.getRequestUser());
		wmsWarehouseBasEntity.setRowStatus(1);
		wmsStorageDao.saveWarehouse(wmsWarehouseBasEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功！");
		return resultBean;
	}

	@Override
	public ResultBean update(WmsWarehouseBasEntity wmsWarehouseBasEntity) {
		ResultBean resultBean = new ResultBean(true, " ");
		WmsWarehouseBasEntity warehouseBasEntity = wmsStorageDao.getById(wmsWarehouseBasEntity.getId());
		if (!warehouseBasEntity.getWarehouseNo().equals(wmsWarehouseBasEntity.getWarehouseNo())) {
			WmsWarehouseBasEntity entityByCode = wmsStorageDao.getByCode(wmsWarehouseBasEntity.getWarehouseNo());
			if (entityByCode != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("仓库编号不唯一！");
				return resultBean;
			}
		}
		if (!warehouseBasEntity.getWarehouseName().equals(wmsWarehouseBasEntity.getWarehouseName())) {
			WmsWarehouseBasEntity entityByName = wmsStorageDao.getByName(wmsWarehouseBasEntity.getWarehouseName());
			if (entityByName != null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("仓库名称不唯一！");
				return resultBean;
			}
		}
		wmsWarehouseBasEntity.setLastUpdateTime(TimeUtil.getLocalDateTime());
		wmsWarehouseBasEntity.setLastUpdateUser(SpringWebUtils.getRequestUser());
		wmsStorageDao.updateWarehouseBasEntity(wmsWarehouseBasEntity);
		resultBean.setSuccess(true);
		resultBean.setMessage("更新成功！");
		return resultBean;
	}

}